package com.baiye.template.controller;

import com.baiye.template.annotation.OperLog;
import com.baiye.template.common.R;
import com.baiye.template.pojo.ExceptionLog;
import com.baiye.template.pojo.OperationLog;
import com.baiye.template.pojo.User;
import com.baiye.template.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/2 10:40
 */
//@RolesAllowed("admin")
@RestController
@Api(value = "日志管理")
@RequestMapping("/log")
public class LogController {
    @Autowired
    private LogService logService;

    @OperLog(operModul = "日志",operType = "get",operDesc = "查询操作日志")
    @Secured("ROLE_USER")
    @RequestMapping(value = "/getOperLog",method = RequestMethod.POST)
    public R getOperLog(@RequestBody OperationLog log,@Param(value = "pageIndex")Integer pageIndex,@Param(value = "pageSize") Integer pageSize){
        //[pageNum, pageSize]  页码  每页显示数量
        List<OperationLog> i = logService.getOperLog(log,pageIndex,pageSize);
        PageInfo<OperationLog> pageInfo = new PageInfo<>(i);
        return new R().ok(pageInfo);
    }

    @RequestMapping(value = "/getExceptionLog",method = RequestMethod.POST)
    public R getExceptionLog(@RequestBody ExceptionLog log,@Param(value = "pageIndex")Integer pageIndex,@Param(value = "pageSize") Integer pageSize){
        List<ExceptionLog> i = logService.getExceptionLog(log,pageIndex,pageSize);
        PageInfo<ExceptionLog> pageInfo = new PageInfo<>(i);
        return new R().ok(pageInfo);
    }
    @RequestMapping(value = "/deleteLog",method = RequestMethod.POST)
    public R deleteLog(@RequestParam(value = "idList")List<Integer> idList,@RequestParam(value = "tableName")String tableName){
        int i = logService.deleteLog(idList,tableName);
        if(i>=1){
            return new R().ok(i);
        }else {
            return new R().error("删除失败");
        }

    }
}
